﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib;

namespace DVSProject.Controllers
{
    public class SetupMachineController : Controller
    {
        //
        // GET: /SetupMachine/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            SetupMachineRepo setupMachineRepo = new SetupMachineRepo();
            IEnumerable<SetupMachineModels> setupMachine = setupMachineRepo.GetSetupMachine();
            return View(setupMachine);
        }

        //
        // GET: /SetupMachine/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        public ActionResult Change(string id)
        {
            SetupMachineRepo setupMachineRepo = new SetupMachineRepo();
            SetupMachineModels setupMachine = setupMachineRepo.GetSetupMachineByLocationSetCodeForChange(id);
            return View(setupMachine);
        }

        //
        // GET: /SetupMachine/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo customerRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> locationSets = customerRepo.GetLocationSet();

            string selectedLocationCode = "";
            IEnumerable<SelectListItem> selectlist = from s in locationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedLocationCode),
                                                         Text = s.LocationSetCode + ' ' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            var setupMachine = new SetupMachineModels { locationSets = locationSets, locationSetSelect = selectlist };
            return View(setupMachine);


        }

        public ActionResult Setup()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        [HttpPost]
        public ActionResult Setup( FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                SetupMachineModels setupMachine = new SetupMachineModels();
                setupMachine.ModelId =Convert.ToInt32(collection["ModelId"]);
                setupMachine.Temp = collection["Temp"];
                setupMachine.SerialNo = collection["SerialNo"];
                setupMachine.RentBasic =Convert.ToInt32( collection["RentBasic"]);
                setupMachine.Remark = collection["Remark"];
                setupMachine.StartMeter =Convert.ToInt32( collection["StartMeter"]);
                setupMachine.CreateBy = (string)Session["User"];
                SetupMachineRepo setupMachineRepo =new SetupMachineRepo();
                bool result = setupMachineRepo.Setup(setupMachine);


                if (result)
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("Setup");
            }
            catch
            {
                return View();
            }
        }




        //
        // POST: /SetupMachine/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                SetupMachineModels setupMachine = new SetupMachineModels();
                setupMachine.LocationSetCode = collection["LocationSetCode"];
                setupMachine.CreateBy = (string)Session["User"];
                SetupMachineRepo setupMachineRepo = new SetupMachineRepo();
                bool result = setupMachineRepo.Create(setupMachine);

                if (result)
                {
                    return RedirectToAction("Setup");
                }
                return RedirectToAction("Create");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SetupMachine()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }




        //
        // GET: /SetupMachine/Edit/5

        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        //
        // POST: /SetupMachine/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SetupMachine/Delete/5

        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        //
        // POST: /SetupMachine/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
